import { sys } from "cc";
import Singleton from "../Base/Singleton";
import { EventEnum, ItemStatusEnum, ItemTypeEnum, SceneEnum, TriggerStatusEnum } from "../Enum";
import EventManger from "./EventManager";

interface IItem{
    status: ItemStatusEnum
    type: ItemTypeEnum
} 
const STORIGEkEY = `STORIGEkEY`


export default class DataManger extends Singleton{
    static get Instance() {
        return super.getInstance<DataManger>()
    }


    private _curItemType: ItemTypeEnum | null = null
    private _isSelect: boolean = false
    private _isMailBoxOpen: boolean
    private _grandmaStatus: TriggerStatusEnum = TriggerStatusEnum.Pending
    private _grandmaDiaIndex = -1
    private _DoorStatus: TriggerStatusEnum = TriggerStatusEnum.Pending
    readonly H2AAnswer = [0, 1, 2, 3, 4, 5, null]
    readonly H2AInitData = [1, 0, 3, 2, 5, 4, null]
    private _H2AData = this.H2AInitData
    private _curScene:SceneEnum=SceneEnum.H1
    private _items: Array<IItem> = [
        { status: ItemStatusEnum.Scene, type: ItemTypeEnum.Key },
        { status: ItemStatusEnum.Disable, type: ItemTypeEnum.Mail },
    ]

    get curItemType() {
        return this._curItemType
    }

    set curItemType(newdata) {
        this._curItemType = newdata
        this.rendorAndSave()
    }

    get isSelect() {
        return this._isSelect
    }

    set isSelect(newdata) {
        this._isSelect = newdata
        this.rendorAndSave()
    }

    get isMailBoxOpen() {
        return this._isMailBoxOpen
    }

    set isMailBoxOpen(newdata) {
        this._isMailBoxOpen = newdata
        this.rendorAndSave()
    }

    get grandmaStatus() {
        return this._grandmaStatus
    }

    set grandmaStatus(newdata) {
        this._grandmaStatus = newdata
        this.rendorAndSave()
    }

    get grandmaDiaIndex() {
        return this._grandmaDiaIndex
    }

    set grandmaDiaIndex(newdata) {
        this._grandmaDiaIndex = newdata
        this.rendorAndSave()
    }

    get DoorStatus() {
        return this._DoorStatus
    }

    set DoorStatus(newdata) {
        this._DoorStatus = newdata
        this.rendorAndSave()
    }

    get H2AData() {
        return this._H2AData
    }

    set H2AData(newdata) {
        this._H2AData = newdata
        this.rendorAndSave()
    }

    get curScene() {
        return this._curScene
    }

    set curScene(newdata) {
        this._curScene = newdata
        this.rendorAndSave()
    }

    get items() {
        return this._items
    }

    set items(newdata) {
        this._items = newdata;
        this.rendorAndSave()
    }

    rendorAndSave() {
        EventManger.Instance.emit(EventEnum.Rendor)
        sys.localStorage.setItem(STORIGEkEY, JSON.stringify(
            {
                curItemType: this.curItemType,
                isSelect: this.isSelect,
                isMailBoxOpen: this.isMailBoxOpen,
                grandmaStatus: this.grandmaStatus,
                grandmaDiaIndex: this.grandmaDiaIndex,
                DoorStatus: this.DoorStatus,
                H2AData: this.H2AData,
                curScene: this.curScene,
                items:this.items,
            }
        ))
    }

    restore() {
        const _data = sys.localStorage.getItem(STORIGEkEY)
        try {
            const data = JSON.parse(_data)
            this.curItemType = data.curItemType
            this.isSelect= data.isSelect
            this.isMailBoxOpen= data.isMailBoxOpen
            this.grandmaStatus= data.grandmaStatus
            this.grandmaDiaIndex= data.grandmaDiaIndex
            this.DoorStatus= data.DoorStatus
            this.H2AData= data.H2AData
            this.curScene= data.curScene
            this.items= data.items
        } catch (e) {
            this.reset()
        }
    }

    reset() {
        this.curItemType = null
        this.isSelect = false
        this.isMailBoxOpen = false
        this.grandmaStatus = TriggerStatusEnum.Pending
        this.grandmaDiaIndex = -1
        this.DoorStatus = TriggerStatusEnum.Pending
        this.H2AData = [...this.H2AInitData]
        this.curScene = SceneEnum.H1
        this.items = [
            { status: ItemStatusEnum.Scene, type: ItemTypeEnum.Key },
            { status: ItemStatusEnum.Disable, type: ItemTypeEnum.Mail },
        ]
    }
}


